<template>
	<view class="page">
		<view class="bg">
			<image src="https://www.shlxkj.info:10010/%E5%81%A5%E5%BA%B7%E6%A1%A3%E6%A1%88/zsafimg/bgimg.png" mode="aspectFit"></image>
		</view>
		<view class="logo">
			<image src="/static/image/logo.png" mode="aspectFit"></image>
		</view>
		<view class="title">
			<image src="/static/image/title.png" mode=""></image>
		</view>
		<view class="welcome">您好，欢迎使用掌上艾防</view>
		<view class="login" v-if="show">
			<text>账号</text>
			<view class="" @click="this.passwordinput=false">
				<uni-easyinput bindinput='getInputValue'  @input="onInputChange" focus input-class="custom-input-class" type="text" v-model="account" placeholder="请输入账号"></uni-easyinput>
			</view>
			<view class="" @click="this.passwordinput=true">
			<text>密码</text>
				<uni-easyinput v-if="this.passwordinput==false" disabled style="margin-top: 30rpx" type="password" v-model="password" placeholder="请输入密码"></uni-easyinput>
				<uni-easyinput v-else focus style="margin-top: 30rpx" type="password" v-model="password" placeholder="请输入密码"></uni-easyinput>
			</view>
			<checkbox-group @change="checkboxChange" class="checkbox">
				<checkbox value="true" :checked="rememberMe" color="#00aeff" style="transform: scale(0.7)" />
				记住密码
			</checkbox-group>
			<view>
				<button class="btnbox" @click="submit">登录</button>
			</view>
			
		</view>
		<view v-else style="margin-top: 200rpx">
			<view class="content">
				<view class="imgbox">
					<image src="/static/image/woshou.png" mode="aspectFit"></image>
				</view>
				<view class="cujin">促进艾滋病主动检测项目</view>
			</view>
			<view class="danwei">
				主持单位：
				<image src="/static/image/red.png" mode="aspectFit"></image>
				中国性病艾滋病防治协会
			</view>
			<button class="btnbox" @click="int">进入</button>
		</view>
		<view class="agree-wrap agree">
			<view style="display: flex; align-items: center" @click="agreeChange">
				<image
					class="radio"
					:src="
						agree === '0'
							? 'https://www.shlxkj.info:10010/%E5%81%A5%E5%BA%B7%E6%A1%A3%E6%A1%88/image/checkbox.png'
							: 'https://www.shlxkj.info:10010/%E5%81%A5%E5%BA%B7%E6%A1%A3%E6%A1%88/image/checkbox-active.png'
					"
					mode="aspectFit"
				/>
				<text class="">同意</text>
			</view>
			<text class="link">《用户协议》</text>
			与
			<text class="link">《隐私政策》</text>
		</view>
		<uni-popup ref="agreement" type="center">
			<view class="agreement-height">
				<image class="agree-bg" style="border-radius: 26rpx" src="https://yun.shlxkj.info:44300/艾瑞测/小程序/static/img/agree-box.png" mode="widthFix"></image>
				<view class="login-content">
					<mp-html :content="content" />
					<view class="agreement-btn-wrap">
						<button class="refuse-btn" @click="onRefuse">拒绝</button>
						<button id="agree-btn" class="ok-btn" @click="handleAgreePrivacyAuthorization" open-type="agreePrivacyAuthorization">同意并继续</button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import store from '@/store/index.js';
import { login, FindLoginUser, GetDictionaryByType } from '@/utils/api.js';
import Copyright from '@/components/copyright/index.vue';
import MpHtml from '@/components/mp-html/mp-html';
export default {
	name: 'Login',
	components: {
		Copyright,
		MpHtml
	},
	data() {
		return {
			show: true,
			gps: false,
			// account: 'admin',
			// password: 'lxkj1213',
			account: '',
			password: '',
			agree: '0',
			content: '',
			navHeight: '',
			statusBarHeight: '',
			passwordinput:false,
			rememberMe: false // 是否记住密码
		};
	},
	watch: {
		// account(newVal) {
		// 	this.account = newVal;
		// 	console.log('当前账号: ', this.account);
		// },
		// password(newVal) {
		// 	this.password = newVal;
		// 	console.log('当前密码: ', this.password);
		// }
	},
	mounted() {},
	onLoad(op) {
		let { statusBarHeight, system } = uni.getSystemInfoSync();
		this.statusBarHeight = statusBarHeight;
		this.navHeight = statusBarHeight + (system.indexOf('iOS') > -1 ? 44 : 48);
		if (op.scan && op.scan === '1') {
			console.log(op);
			this.isNeedScan = true;
		} else {
			this.isNeedScan = false;
		}
	},
	onShow() {
		// 获取存储的账号和密码
		const storedUsername = uni.getStorageSync('account');
		const storedPassword = uni.getStorageSync('password');
		
		console.log('账号密码', storedUsername, storedPassword);
		if (storedUsername && storedPassword) {
			this.account = storedUsername;
			this.password = storedPassword;
			this.rememberMe = true; // 如果有存储信息，默认勾选记住密码
		}
	},
	methods: {
		checkboxChange(e) {
			this.rememberMe = e.detail.value[0]; // 通过 e.detail.value 来更新 rememberMe
		},
		getInputValue(e){
		  console.log(e.detail)// {value: "ff", cursor: 2}  
		},
		async agreeChange() {
			console.log(this.agree);
			if (this.agree === '0') {
				if (!this.content) {
					await this.getData();
				} else {
					this.$refs.agreement.open('center');
				}
			}else{
				this.agree = '0'
			}
		},
		// 获取用户协议
		getData() {
			// this.$refs.agreement.open('center');
			uni.showLoading({
				title: 'loading'
			});
			GetDictionaryByType({
				Type: 'UserAgreement'
			})
				.then((res) => {
					if (res.code == 200) {
						uni.hideLoading();
						this.content = decodeURIComponent(res.data[0].value).replace(/\+style/g, ' style');
						this.$refs.agreement.open('center');
					}
				})
				.catch((error) => {
					uni.hideLoading();
					console.error('请求失败', error);
					// 在这里处理请求失败的逻辑
					uni.showToast({
						title: '网络信号不好，请稍候再试',
						icon: 'none'
					});
				});
		},
		handleAgreePrivacyAuthorization() {
			this.$refs.agreement.close();
			this.agree = '1';
			console.log('用户点击同意');
			wx.getPrivacySetting({
				success: (res) => {
					console.log(res); // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
					if (res.needAuthorization) {
						// 需要弹出隐私协议
						console.log('需要弹出隐私协议');
						this.getData();
						this.$refs.agreement.open('center');
					} else {
						// 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用隐私接口
						console.log('用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用隐私接口');
					}
				}
				// fail: () => {},
				// complete() => {}
			});
		},
		onRefuse() {
			this.$refs.agreement.close();
			this.agree = '0';
		},
		onInputChange(e) {
			console.log(e);
				this.account = e;
				// console.log('this.account:', this.account);
			// setTimeout(() => {
			// 	this.account = e.detail.value;
			// 	console.log('this.account:', this.account);
			// }, 500);
		},
		async FindLoginUser() {
			const res = await FindLoginUser();
			this.$store.commit('$uStore', {
				name: 'vx_uid',
				value: res.data.userid
			});
			if (res.data.cdc == 1 || res.data.cdc == 2) {
				this.show = false;
			} else {
				uni.reLaunch({
					url: '/pages/outcome/index'
				});
			}
		},
		//提交登录信息
		async submit() {
			// console.log('账号值',accountValue); // 输出输入框的值
			if (this.agree === '0') {
				uni.showToast({
					title: '请先同意用户协议和隐私政策',
					icon: 'none'
				});
				return;
			}
			if (this.account === '') {
				uni.showToast({
					title: '请填写账号',
					icon: 'none'
				});
				return;
			}
			if (this.password === '') {
				uni.showToast({
					title: '请填写密码',
					icon: 'none'
				});
				return;
			}
			if (this.rememberMe) {
				console.log('记住密码功能', this.account, this.password);
				// 记住密码功能
				uni.setStorageSync('account', this.account);
				uni.setStorageSync('password', this.password); // 注意密码的安全性，最好加密存储
			} else {
				uni.removeStorageSync('username');
				uni.removeStorageSync('password');
			}
			uni.showLoading({
				title: '登录中..'
			});
			setTimeout(async () => {
				console.log('账号', this.account);

				try {
					const res = await login({
						account: this.account,
						passWord: this.password
					});
					// const res = await login({
					// 	account: 'shanghai',
					// 	passWord: 'shanghai001'
					// });
					// const res = await login({
					// 	account: 'admin',
					// 	passWord: 'lxkj1213'
					// });

					if (res.code == 200) {
						this.$store.commit('$uStore', {
							name: 'vx_token',
							value: res.data
						});
						this.FindLoginUser();
						uni.hideLoading();
						uni.showToast({
							title: '登录成功',
							duration: 2000
						});
						this.account = '';
						this.password = '';
					} else {
						uni.hideLoading();
						uni.showToast({
							title: '登录失败',
							duration: 2000,
							icon: 'error'
						});
						this.account = '';
						this.password = '';
					}
				} catch (error) {
					uni.hideLoading();
					uni.showToast({
						title: '发生错误，请重试',
						duration: 2000,
						icon: 'error'
					});
					this.account = '';
					this.password = '';
				}
			}, 50);
		},
		int() {
			uni.reLaunch({
				url: '/pages/outcome/index'
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.page {
	height: 100vh;
	position: relative;
	// background-image: url('https://www.shlxkj.info:10010/%E5%81%A5%E5%BA%B7%E6%A1%A3%E6%A1%88/zsafimg/bgimg.png');
	// background-size: cover;
	// background-repeat: no-repeat;
	overflow: hidden;
	.agree-wrap {
		width: 470rpx;
		margin: 50rpx auto 0;
		display: flex;
		align-items: center;

		text {
			font-size: 25rpx;
		}
		.radio {
			width: 27rpx;
			height: 27rpx;
			margin-right: 10rpx;
		}
	}
	.bg {
		width: 100%;
		height: 100vh;
		position: absolute;
		top: 0%;
		left: 0;
		z-index: -1;

		image {
			width: 100%;
			height: 100vh;
		}
	}

	.logo {
		width: 310rpx;
		height: 100rpx;
		margin: 170rpx auto 0;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.title {
		width: 200rpx;
		height: 60rpx;
		margin: 40rpx auto 0;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.welcome {
		margin-top: 60rpx;
		width: 100%;
		text-align: center;
		font-weight: 400;
		font-size: 36rpx;
		// color: #222222;
		color: #00aeff;
		line-height: 36rpx;
	}

	.login {
		width: 70%;
		height: 660rpx;
		padding: 60rpx;
		margin: 40rpx auto 0;
		background: #ffffff;
		border-radius: 23rpx 23rpx 23rpx 23rpx;

		text {
			margin-bottom: 20rpx;
			font-weight: 500;
			font-size: 36rpx;
			color: #222222;
			line-height: 36rpx;
		}
	}

	.btnbox {
		width: 95%;
		height: 105rpx;
		margin: 100rpx auto 0;
		color: #ffffff;
		font-weight: 500;
		font-size: 40rpx;
		background: linear-gradient(90deg, #a4edf1 0%, #00aeff 100%);
		box-shadow: 0rpx 8rpx 15rpx 0rpx rgba(14, 98, 122, 0.15);
		border-radius: 12rpx 12rpx 12rpx 12rpx;
	}

	.content {
		width: 60%;
		margin: 0 auto;
		text-align: center;
		color: #00aeff;
		font-weight: 600;
		font-size: 46rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.imgbox {
			width: 126rpx;
			height: 118rpx;

			image {
				vertical-align: middle;
				width: 100%;
				height: 100%;
			}
		}

		.cujin {
		}
	}

	.danwei {
		margin: 50rpx auto 80rpx;
		// background-image: url('/static/image/kuang.png');
		width: 90%;
		height: 34rpx;
		line-height: 34rpx;
		text-align: center;
		font-size: 34rpx;
		color: #9da4a8;
		vertical-align: middle;

		image {
			vertical-align: middle;
			width: 30rpx;
			height: 40rpx;
		}
	}

	.agreement-height {
		width: 615rpx;
		height: 70vh;
		margin: 0 auto;
		border-radius: 26rpx;
		background-color: #ffffff;
		position: relative;

		.agree-bg {
			width: 615rpx;
			position: absolute;
			z-index: 0;
		}

		.login-content {
			width: 515rpx;
			position: absolute;
			padding: 60rpx 50rpx 0;
			height: calc(100% - 100rpx);
			overflow-y: scroll;
		}

		.agreement-btn-wrap {
			background-color: #ffffff;
			padding-top: 20rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.ok-btn {
				width: 250rpx;
				height: 85rpx;
				line-height: 85rpx;
				background: #00aeff;
				border-radius: 15rpx;
				text-align: center;
				font-size: 31rpx;
				font-weight: 400;
				color: #ffffff;
			}

			.refuse-btn {
				width: 250rpx;
				height: 85rpx;
				line-height: 85rpx;
				font-size: 31rpx;
				font-weight: 400;
				color: #333333;
				background: #ebebed;
				text-align: center;
				border-radius: 15rpx;
			}
		}
	}
}

::v-deep .uni-easyinput__content {
	margin: 40rpx 0;
	background-color: #eef8ff !important;
	border-radius: 12rpx 12rpx 12rpx 12rpx;
}

::v-deep .uni-easyinput__content-input {
	height: 84rpx;
}

::v-deep .is-input-border {
	border: none;
}
</style>
